package cn.edu.neusoft.food229.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

import cn.edu.neusoft.food229.R;
import cn.edu.neusoft.food229.activity.SearchActivity;
import cn.edu.neusoft.food229.datebase.RecordDBOperation;
import cn.edu.neusoft.food229.datebase.RecordSQLiteOpenHelper;

public class Fragment3 extends FragmentBase_229{
    private Context context;
    private View view;
    private ImageButton imageButton;
    private EditText editText;
    private Button button;
    private TextView tv_clear,tv_tip;
    private RecordDBOperation recordDB;
    private ListView listView;
    public Fragment3() {
        // Required empty public constructor
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment3,container,false);
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        System.out.println("-----f3-----");
        RecordSQLiteOpenHelper helper = new RecordSQLiteOpenHelper(this.getActivity());
        recordDB = new RecordDBOperation(helper);
        initEvents(view);


    }

    private void initEvents(View view) {
        imageButton=view.findViewById(R.id.imgbtn_clear);
        editText=view.findViewById(R.id.et_search);
        button=view.findViewById(R.id.button_ok);
        tv_tip=view.findViewById(R.id.tv_tip);
        listView=view.findViewById(R.id.listView);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recordDB.deleteData();
                showData("");
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
        String s=editText.getText().toString();
        Intent intent=new Intent(context, SearchActivity.class);
        Bundle bundle=new Bundle();
        bundle.putString("search",s);
        intent.putExtras(bundle);
        context.startActivity(intent);
                recordDB.insertData(editText.getText().toString().trim());
              showDetailInfo(editText.getText().toString().trim());
            }
        });
        editText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){
                    ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                            getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    boolean hasData = recordDB.hasData(editText.getText().toString().trim());
                    if (!hasData) {
                        recordDB.insertData(editText.getText().toString().trim());
                        showData("");
                    }
                   showDetailInfo(editText.getText().toString().trim());
                }

                    return false;
            }
        });
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {


        }         @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

        }         @Override
            public void afterTextChanged(Editable s) {
                button.setVisibility(View.VISIBLE);
                if (s.toString().trim().length() == 0) {
                    tv_tip.setText("搜索历史");             }
                else {
                    tv_tip.setText("搜索结果");             }
                    String tempName = editText.getText().toString();
               showData(tempName);

        }     });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            TextView textView = (TextView) view.findViewById(android.R.id.text1);
            String name = textView.getText().toString();
            editText.setText(name);
            showDetailInfo(name);
            }     }); }




    private void showDetailInfo(String trim) {
        //跳转到搜索结果处理页面
//        Intent intent=new Intent(context, SearchActivity.class);
//        context.startActivity(intent);
//        String s=editText.getText().toString();
//        Intent intent=new Intent(context, SearchActivity.class);
//        Bundle bundle=new Bundle();
//        bundle.putString("search",s);
//        intent.putExtras(bundle);
//        context.startActivity(intent);

    }

    private void showData(String tempName) {
        List list = recordDB.queryData(tempName);
        // 创建 adapter适配器对象
        SimpleAdapter adapter = new SimpleAdapter(
                getActivity(), list, android.R.layout.simple_list_item_1,
                new String[]{"name"}, new int[]{android.R.id.text1});
        listView.setAdapter(adapter);     adapter.notifyDataSetChanged();

    }

}
